package com.ruoyi.quartz.task;


import com.snowRemovingUser.util.FileDownloadUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Component("hsomv")
public class HsoMvTask {


    /** log */
    protected Logger logger = LoggerFactory.getLogger(this.getClass());



    @Value("${linux.localInspectFilePath}")
    private String localInspectFilePath;

    @Value("${linux.remoteFilePath}")
    private String remoteFilePath;

    @Value("${linux.localFilePath}")
    private String localFilePath;

    @Value("${linux.username}")
    private String username;

    @Value("${linux.password}")
    private String password;

    @Value("${linux.ip}")
    private String ip;

    @Value("${linux.port}")
    private String port;





    public void start(){


        StringBuffer hsoName  = new StringBuffer();
        hsoName.append("hsoIndex");

        LocalDateTime now = LocalDateTime.now();

        int year = now.getYear();
        int monthValue = now.getMonthValue();
        String month = String.format("%02d", monthValue);
        int day = now.getDayOfMonth();
        int hour = now.getHour();
        int minute = now.getMinute();


        // 判断现在是几点 并且 大于20分钟
        if (hour == 4 && minute >=20){
            // 拼接文件名
            hsoName.append(year);
            hsoName.append(month);
            day = day-1;
            String dayStr = String.format("%02d", day);
            hsoName.append(dayStr);
            hsoName.append("21z");
            hsoName.append(".tar.gz");
        }else if (hour == 7 && minute >=20){

            // 拼接文件名
            hsoName.append(year);
            hsoName.append(month);
            hsoName.append(day);
            hsoName.append("00z");
            hsoName.append(".tar.gz");
        }else if (hour == 8 && minute >=20){

            // 拼接文件名
            hsoName.append(year);
            hsoName.append(month);
            hsoName.append(day);
            hsoName.append("01z");
            hsoName.append(".tar.gz");
        }else if (hour == 11 && minute >=20){

            // 拼接文件名
            hsoName.append(year);
            hsoName.append(month);
            hsoName.append(day);
            hsoName.append("04z");
            hsoName.append(".tar.gz");
        }else if (hour == 12 && minute >=20){

            // 拼接文件名
            hsoName.append(year);
            hsoName.append(month);
            hsoName.append(day);
            hsoName.append("05z");
            hsoName.append(".tar.gz");
        }else if (hour == 15 && minute >=20){

            // 拼接文件名
            hsoName.append(year);
            hsoName.append(month);
            hsoName.append(day);
            hsoName.append("08z");
            hsoName.append(".tar.gz");
        }else if (hour == 16 && minute >=20){

            // 拼接文件名
            hsoName.append(year);
            hsoName.append(month);
            hsoName.append(day);
            hsoName.append("09z");
            hsoName.append(".tar.gz");
        }else if (hour == 21 && minute >=20){

            // 拼接文件名
            hsoName.append(year);
            hsoName.append(month);
            hsoName.append(day);
            hsoName.append("14z");
            hsoName.append(".tar.gz");
        }else if (hour == 23 && minute >=20){

            // 拼接文件名
            hsoName.append(year);
            hsoName.append(month);
            hsoName.append(day);
            hsoName.append("16z");
            hsoName.append(".tar.gz");
        }else {
            logger.info("时间不满足，无需拉取hso文件");
            return;
        }

        // 文件路径
        String remoteFilePathName =  remoteFilePath + hsoName;

        // 获取 本地文件夹
        File folder = new File(localInspectFilePath);
        File[] files = folder.listFiles();
        assert files != null;
        List<String> fileNames = Arrays.stream(files).filter(File::isFile).map(File::getName).collect(Collectors.toList());

        boolean downloadFile = false;
        if (!fileNames.contains(hsoName.toString())){

            //连接到linux远程处理文件
            downloadFile = FileDownloadUtil.downloadFile(remoteFilePathName, localFilePath, username, password, ip, Integer.parseInt(port));

            if (!downloadFile){
                throw new RuntimeException("一键处理异常失败");
            }else {
                logger.info("拉取hso文件成功");
            }

        }else {
            logger.info("hso文件已存在，无需再次拉取");
        }
    }






























}
